package com.components.project.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.components.project.R
import com.components.project.databean.NewsDataBean
import com.components.project.web.IntentManager

/**
 * @Author LuoKun
 * @Des
 */
class NewsAdapter : RecyclerView.Adapter<NewsAdapter.ViewHolder> {


    private var mUserChangeHeadImage =
        "https://img2.baidu.com/it/u=3895119537,2684520677&fm=253&fmt=auto&app=138&f=JPEG?w=80&h=80"

    private var mContext: Context

    private var mDataLists: MutableList<NewsDataBean>

    constructor(mContext: Context, mDataLists: MutableList<NewsDataBean>) : super() {
        this.mContext = mContext
        this.mDataLists = mDataLists
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        var view =
            LayoutInflater.from(mContext).inflate(R.layout.item_tools_recycler, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val newsDataBean = mDataLists[position]
        holder.tvTitle.text = newsDataBean.title
        holder.tvPageviews.text = newsDataBean.pagesview
        holder.tvCreateTime.text = newsDataBean.createTime
        Glide.with(mContext)
            .load(newsDataBean.image)
            .into(holder.ivRightImg)
        holder.itemView.setOnClickListener {
            IntentManager.intentWebView(mDataLists[position])
            notifyItemChanged(position, mDataLists[position])
        }
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int, payloads: MutableList<Any>) {
        if (payloads != null && payloads.size > 0) {
            mDataLists[position].title = "我是修改过后的新闻标题"
            mDataLists[position].createTime = "2022-4-28"
            mDataLists[position].pagesview = "浏览量 ${666}"
            mDataLists[position].image = mUserChangeHeadImage
        }
        onBindViewHolder(holder, position)
    }

    override fun getItemCount(): Int {
        return mDataLists.size
    }

    inner class ViewHolder : RecyclerView.ViewHolder {
        var tvTitle: TextView
        var tvPageviews: TextView
        var tvCreateTime: TextView
        var ivRightImg: ImageView

        constructor(itemView: View) : super(itemView) {
            tvTitle = itemView.findViewById(R.id.tvTitle)
            tvPageviews = itemView.findViewById(R.id.tvPageviews)
            tvCreateTime = itemView.findViewById(R.id.tvCreateTime)
            ivRightImg = itemView.findViewById(R.id.ivRightImg)
        }
    }

}